﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace ErrorProviders
{
    public class DockPanelErrorProvider : DockPanel, IInternalErrorProvider, IErrorProvider
    {

        #region Attached property and property ErrorMesage

        public static readonly DependencyProperty ErrorMessageProperty = DependencyProperty.RegisterAttached("ErrorMessage", typeof(string), typeof(DockPanelErrorProvider),
            new FrameworkPropertyMetadata("",
                new PropertyChangedCallback(OnErrorMessageChanged)
                ));

        [AttachedPropertyBrowsableForChildren(IncludeDescendants = true)]
        public static string GetErrorMessage(UIElement element)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            return (string)element.GetValue(ErrorMessageProperty);
        }

        public static void SetErrorMessage(UIElement element, string value)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            element.SetValue(ErrorMessageProperty, value);
        }

        public string ErrorMessage
        {
            get
            {
                return (string)this.GetValue(ErrorMessageProperty);
            }

            set
            {
                this.SetValue(ErrorMessageProperty, value);
            }
        }

        private static void OnErrorMessageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }
        #endregion

        #region Attached property and property ErrorPropertyModified
        public static readonly DependencyProperty ErrorPropertyModifiedProperty = DependencyProperty.RegisterAttached("ErrorPropertyModified", typeof(string), typeof(DockPanelErrorProvider),
            new FrameworkPropertyMetadata("",
                new PropertyChangedCallback(OnErrorPropertyModifiedChanged)));

        [AttachedPropertyBrowsableForChildren(IncludeDescendants = true)]
        public static string GetErrorPropertyModified(UIElement element)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            return (string)element.GetValue(ErrorPropertyModifiedProperty);
        }

        public static void SetErrorPropertyModified(UIElement element, string value)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            element.SetValue(ErrorPropertyModifiedProperty, value);
        }

        public string ErrorPropertyModified
        {
            get
            {
                return (string)GetValue(ErrorPropertyModifiedProperty);
            }

            set
            {
                this.SetValue(ErrorPropertyModifiedProperty, value);
            }
        }

        private static void OnErrorPropertyModifiedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }
        #endregion

        #region Attached property and property ErrorValue
        public static readonly DependencyProperty ErrorValueProperty = DependencyProperty.RegisterAttached("ErrorValue", typeof(object), typeof(DockPanelErrorProvider),
           new PropertyMetadata(null));

        [AttachedPropertyBrowsableForChildren(IncludeDescendants = true)]
        public static object GetErrorValue(UIElement element)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            return element.GetValue(ErrorValueProperty);
        }

        public static void SetErrorValue(UIElement element, object value)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            element.SetValue(ErrorValueProperty, value);
        }

        public object ErrorValue
        {
            get
            {
                return GetValue(ErrorValueProperty);
            }

            set
            {
                SetValue(ErrorValueProperty, value);
            }
        }

        #endregion

        #region Attached property and property ValidateMethod
        public static readonly DependencyProperty ValidateMethodProprety = DependencyProperty.RegisterAttached("ValidateMethod", typeof(ValidateDataCallback), typeof(DockPanelErrorProvider),
            new PropertyMetadata(null));

        [AttachedPropertyBrowsableForChildren(IncludeDescendants = true)]
        public static ValidateDataCallback GetValidateMethod(UIElement element)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            return (ValidateDataCallback)element.GetValue(ValidateMethodProprety);
        }

        public static void SetValidateMethod(UIElement element, ValidateDataCallback value)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            element.SetValue(ValidateMethodProprety, value);
        }

        public ValidateDataCallback ValidateMethod
        {
            get
            {
                return (ValidateDataCallback)GetValue(ValidateMethodProprety);
            }

            set
            {
                this.SetValue(ValidateMethodProprety, value);
            }
        }

        #endregion

        private Dictionary<UIElement, InternalObjectProperties> oldValues;

        #region Constructors

        /// <summary>
        /// Constructeur par défaut
        /// </summary>
        public DockPanelErrorProvider()
            : base()
        {
            this.oldValues = new Dictionary<UIElement, InternalObjectProperties>();
        }


        #endregion        

        #region IErrorProvider Members

        public Dictionary<System.Windows.UIElement, InternalObjectProperties> OldValues
        {
            get { return this.oldValues; }
        }

        public ValidateDataCallback InternalGetValidateMethod(System.Windows.UIElement element)
        {
            return GetValidateMethod(element);
        }

        public string InternalGetErrorMessage(System.Windows.UIElement element)
        {
            return GetErrorMessage(element);
        }

        public object InternalGetErrorValue(System.Windows.UIElement element)
        {
            return GetErrorValue(element);
        }

        public System.Windows.DependencyProperty InternalErrorPropertyModifiedProperty
        {
            get { return ErrorPropertyModifiedProperty; }
        }

        public string InternalGetErrorPropertyModified(System.Windows.UIElement element)
        {
            return GetErrorPropertyModified(element);
        }

        #endregion

        #region IErrorProvider Members

        public void ValidateContent()
        {
            ValidateContent(this);
        }

        public void ValidateContent(UIElement element)
        {
            ErrorProviderHelper erh = new ErrorProviderHelper();
            erh.ValidateUIElement(this, element);
        }

        public void RestoreDefaultValue()
        {
            RestoreDefaultValue(this);
        }

        public void RestoreDefaultValue(UIElement element)
        {
            ErrorProviderHelper erh = new ErrorProviderHelper();
            erh.RestoreDefaultValue(this, element);
        }

        #endregion
    }
}
